﻿using System.Drawing;
using JamesZhao.Engine.GameComponents;
using JamesZhao.Engine.GameObjects;
using JamesZhao.Engine.Graphics.Geometry;
using JamesZhao.Engine.Graphics.RenderStages;
using JamesZhao.Engine.Loaders;
using JamesZhao.Engine.Utils;
using  System.Linq;
using SlimMath;

namespace JamesZhao.Engine
{
    public class Program
    {
        private static void Main()
        {
            //            var sp = new Vector3(0, 0, 0);
            //            var n = new Vector3(0, 1, 0);
            //
            //            var l0 = new Vector3(1, 1, 0);
            //            var l1 = new Vector3(-5, 1, 0);
            //
            //            return;
            //            try
            //            {

            var ge = new GameEngine();
            //return;
            ge.Initialize();
            var go = new GameObject(ge.SceneManager.Root);
            var rc = new MeshComponent(go)
                         {
                             Mesh = new ObjMeshloader().LoadModel("Meshes/test.obj")
                         };
            
            var camGo = new GameObject(ge.SceneManager.Root);
            var constantMovementComp = new ConstantMovement(camGo);
            //var mc = new CameraMovementComponent(camGo);
            var camComp = new CameraComponent(camGo);
            var dfpsComp = new DisplayFpsComponent(go);
            camGo.Transform.LocalPosition = new Vector3(-10, -2, -30);
            float y = 90;
            CreateTestLight(ge, new Vector3(0, y, 0), true, 10000);
            //CreateTestLight(ge, new Vector3(2, y, 0), true, 200);
            //CreateTestLight(ge, new Vector3(0, y, -2), true, 200);
            //CreateTestLight(ge, new Vector3(0, y, 2));

            for (int i = 0; i < 30; i++)
            {
                //CreateTestLight(ge, new Vector3(RandomUtils.RandInt(-50, 50), 10, RandomUtils.RandInt(-50, 50)), false);
            }
            var rpv = new RenderPipelineVisualizer
                          {
                              Pipeline = ge.GraphicsManager.MainViewport.Pipeline,
                              Output = ge.GraphicsManager.MainViewport.Pipeline.ColorSurface
                          };

            var steerComponent = new SteerComponent(camGo);

            steerComponent.Target = new Vector3(1000, 70, 5000);

            steerComponent.ReadGpuValueStage =
                (ReadGpuValueStage)
                ge.GraphicsManager.MainViewport.Pipeline.RenderStages.Where(x => x is ReadGpuValueStage).First();
            rpv.InitalizeEntries();
            rpv.Show();
            ge.GraphicsManager.MainViewport.Size = new Size(512, 512);
            ge.Run();
        }

        private static void CreateTestLight(GameEngine ge, Vector3 pos, bool isShadowed, float intensity = .2f)
        {
            var light = new GameObject(ge.SceneManager.Root);

            var lightCo = new LightComponent(light);
            lightCo.Transform.LocalPosition = pos;
            lightCo.Transform.LocalRotation = Quaternion.RotationYawPitchRoll(
                MathExtended.DegreesToRadians(35),
                MathExtended.DegreesToRadians(90), 0);
            var smc = new SimpleMoveComponent(light);
            lightCo.Intensity = intensity;
            lightCo.IsShadowed = isShadowed;
        }
    }
}